﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MSI = Microsoft.Deployment.WindowsInstaller;
using IO = System.IO;
using SLP;

namespace MsiCustomize
{
    /// <summary>
    /// Interaction logic for MsiProperties.xaml
    /// </summary>
    public partial class MsiProperties : UserControl
    {
        /// <summary>
        /// Local object to make working with Customization Project Class easier
        /// </summary>
        private MstProject MSICP { get { try { return (MstProject)this.DataContext; } catch { return null; } } }

        public MsiProperties()
        {
            InitializeComponent();
            this.DataContextChanged += MsiProperties_DataContextChanged;            
        }

        private void MsiProperties_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (MSICP!=null)
                MSICP.PropertyChanged += MSICP_PropertyChanged;
        }

        private void MSICP_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
        }

        private void SelectWorkingFolder(object sender, RoutedEventArgs e)
        {

        }

        private void SelectMSIFilename(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Title = "Select MSI to Customize";
            dlg.Filter = "MSI Files|*.msi|All Files|*.*";
            dlg.FilterIndex = 0;
            if (dlg.ShowDialog(Application.Current.MainWindow) == true)
            {
                MSICP.MsiFileName = IO.Path.GetFileName(dlg.FileName);
                MSICP.MsiFolder = IO.Path.GetDirectoryName(dlg.FileName);
                MSICP.ProjectProperties.Clear();                
                LoadProperties(false);
            }       
        }

        private void LoadProperties(bool PromptForConflicts = false)
        {
            try
            {
                using (MSI.Database msi = new MSI.Database(IO.Path.Combine(MSICP.MsiFolder, MSICP.MsiFileName),
                        MSI.DatabaseOpenMode.ReadOnly))
                {
                    using (var view = msi.OpenView("SELECT * FROM Property"))
                    {
                        view.Execute();
                        foreach (var rec in view)
                        {
                            string PName = rec.GetString("Property");
                            string PValue = rec.GetString("Value");
                            MSICP.ProjectProperties.Add(new MsiPropertyItem(PName, PValue));
                        }
                    }
                }
            }
            catch { }
        }

        private void ClearAll_Click(object sender, RoutedEventArgs e)
        {
            MSICP.ProjectProperties.Clear();
        }

        private void LoadFromMSI_Click(object sender, RoutedEventArgs e)
        {
            LoadProperties(true);
        }

        private void NewProperty_Click(object sender, RoutedEventArgs e)
        {
            // prompt for new property
        }
    }
}
